
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:onCreate()
    -- add background image
    display.newSprite("HelloWorld.png")
        :move(display.center)
        :addTo(self)

    self:addProto()

    self:addTableView()
end

function MainScene:addProto()
    -- add HelloWorld label
    local hwLabel = cc.Label:createWithSystemFont("Hello World", "Arial", 40)
        :move(display.cx, display.cy + 200)
        :addTo(self)


    local pbFilePath = cc.FileUtils:getInstance():fullPathForFilename("MsgProtocol.pb")
    release_print("PB file path: "..pbFilePath)
    
    local buffer = read_protobuf_file_c(pbFilePath)
    protobuf.register(buffer) --注:protobuf 是因为在protobuf.lua里面使用module(protobuf)来修改全局名字
    
    local stringbuffer = protobuf.encode("Person",      
        {      
            name = "Alice",      
            id = 12345,      
            phone = {      
                {      
                    number = "87654321"      
                },      
            }      
        })           
    
    
    local slen = string.len(stringbuffer)
    release_print("slen = "..slen)
    
    local temp = ""
    for i=1, slen do
        temp = temp .. string.format("0xX, ", string.byte(stringbuffer, i))
    end
    release_print(temp)
    local result = protobuf.decode("Person", stringbuffer)
    release_print("result name: "..result.name)
    release_print("result name: "..result.id)

    hwLabel:setString(result.name)
end

function MainScene.scrollViewDidScroll(view)
    --print("scrollViewDidScroll")
end

function MainScene.scrollViewDidZoom(view)
    print("scrollViewDidZoom")
end

function MainScene.tableCellTouched(table,cell)
    print("cell touched at index: " .. cell:getIdx())
    dump(cell)
    --dump(cell:getChildByName(111222))
end

function MainScene.cellSizeForTable(table,idx) 
    return 60,60
end

function MainScene.tableCellAtIndex(table, idx)
    local strValue = string.format("%d",idx)
    local cell = table:dequeueCell()
    local label = nil

    local function callback(sender, eventType)
        print("这是按钮点击", sender:getTag(), eventType)
    end

    if nil == cell then
        cell = cc.TableViewCell:new()
        -- cell:setSwallowTouches(false)
        local sprite = cc.Sprite:create("Icon.png")
        sprite:setAnchorPoint(cc.p(0,0))
        sprite:setPosition(cc.p(0, 0))
        cell:addChild(sprite)

        label = cc.Label:createWithSystemFont(strValue, "Helvetica", 20.0)
        label:setPosition(cc.p(0,0))
        label:setAnchorPoint(cc.p(0,0))
        label:setTag(123)
        cell:addChild(label)

        local resetBtn = ccui.Button:create("Icon.png", "Icon.png")
        resetBtn:setTouchEnabled(true)
        resetBtn:setSwallowTouches(false)
        resetBtn:setTitleText("Visit URL")
        resetBtn:setAnchorPoint(cc.p(0,0))
        resetBtn:setPosition(cc.p(0, 0))
        resetBtn:setName("111222")
        resetBtn:setTag(idx)
        resetBtn:addTouchEventListener(callback)
        cell:addChild(resetBtn)

        --local layer = cc.Layer:create()

        print("新建cell", idx)
    else
        label = cell:getChildByTag(123)
        if nil ~= label then
            label:setString(strValue)
        end

        print("更新cell", idx)

        -- local resetBtn = cell:getChildByName(111222)
        -- resetBtn:setTag(idx)
        -- --dump(cell:getChildByName(111222))
        -- resetBtn:addTouchEventListener(callback)

    end

    return cell
end

function MainScene.numberOfCellsInTableView(table)
   return 25
end

function MainScene:addTableView()

    local winSize = cc.Director:getInstance():getWinSize()

    local tableView = cc.TableView:create(cc.size(600,60))
    tableView:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL)
    tableView:setPosition(cc.p(20, winSize.height / 2 - 150))
    tableView:setDelegate()
    self:addChild(tableView)
    --registerScriptHandler functions must be before the reloadData funtion
    tableView:registerScriptHandler(MainScene.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW)  
    tableView:registerScriptHandler(MainScene.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
    tableView:registerScriptHandler(MainScene.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
    tableView:registerScriptHandler(MainScene.tableCellTouched,cc.TABLECELL_TOUCHED)
    tableView:registerScriptHandler(MainScene.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX)
    tableView:registerScriptHandler(MainScene.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX)
    tableView:reloadData()
    -- tableView:setSwallowTouches(false)
--[[
    tableView = cc.TableView:create(cc.size(60, 350))
    tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL)
    tableView:setPosition(cc.p(winSize.width - 150, winSize.height / 2 - 150))
    tableView:setDelegate()
    tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN)
    self:addChild(tableView)
    tableView:registerScriptHandler(MainScene.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL)
    tableView:registerScriptHandler(MainScene.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM)
    tableView:registerScriptHandler(MainScene.tableCellTouched,cc.TABLECELL_TOUCHED)
    tableView:registerScriptHandler(MainScene.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX)
    tableView:registerScriptHandler(MainScene.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX)
    tableView:registerScriptHandler(MainScene.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
    tableView:reloadData()
    -- tableView:setSwallowTouches(false)
]]
    
    -- Back Menu
    -- local pToMainMenu = cc.Menu:create()
    -- CreateExtensionsBasicLayerMenu(pToMainMenu)
    -- pToMainMenu:setPosition(cc.p(0, 0))
    -- self:addChild(pToMainMenu,10)

    return true
end

return MainScene
